package com.ruoyi.web.controller.user;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.translate.TransApi;
import com.ruoyi.common.utils.translate.TranslateUtil;
import com.ruoyi.system.domain.config.VsConfig;
import com.ruoyi.system.service.config.IVsConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.user.VsDramasUserWalletApply;
import com.ruoyi.system.service.user.IVsDramasUserWalletApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户提现Controller
 * 
 * @author zzx
 * @date 2024-07-06
 */
//@Api(tags = "用户提现")
@RestController
@RequestMapping("/user/apply")
public class VsDramasUserWalletApplyController extends BaseController
{
    @Autowired
    private IVsDramasUserWalletApplyService vsDramasUserWalletApplyService;

    @Autowired
    private IVsConfigService vsConfigMapper;

    /**
     * 查询用户提现列表
     */
    @ApiOperation("查询用户提现列表")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, VsDramasUserWalletApply vsDramasUserWalletApply)
    {
        if (getLoginUser().getUserId() != null) {
            startPage();
            List<VsDramasUserWalletApply> list = vsDramasUserWalletApplyService.selectVsDramasUserWalletApplyList(vsDramasUserWalletApply);
            return getDataTable(list);
        }
        VsUser user = getLoginUser().getVsUser();
        if (user == null) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("暂未登录！");
            return tableDataInfo;
        }
        vsDramasUserWalletApply.setUserId(user.getId());
        startPage();
        List<VsDramasUserWalletApply> list = vsDramasUserWalletApplyService.selectVsDramasUserWalletApplyList(vsDramasUserWalletApply);
        String lang = "zh";
        if (!StringUtils.isEmpty(request.getHeader("lang"))) {
            lang = request.getHeader("lang");
        }
        for (VsDramasUserWalletApply a : list) {
            String method = "";
            String status = "";
            if ("bank".equals(a.getApplyType())) {
                method = "银行卡";
            } else if ("alipay".equals(a.getApplyType())) {
                method = "支付宝";
            } else if ("wechat".equals(a.getApplyType())) {
                method = "微信";
            }
            if (a.getStatus() == 0) {
                status = "待审核";
            } else if (a.getStatus() == 1) {
                status = "已同意";
            } else if (a.getStatus() == 2) {
                status = "已打款";
            } else if (a.getStatus() == -1) {
                status = "已拒绝";
            }
            if (!"zh".equals(lang)) {
                String s = batchTranslate(method, lang);
                String t = batchTranslate(status, lang);
                a.setApplyTypeText(s);
                a.setStatusText(t);
            } else {
                a.setApplyTypeText(method);
                a.setStatusText(status);
            }
        }
        return getDataTable(list);
    }

    private String batchTranslate(String s, String languageType) {
        VsConfig config = new VsConfig();
        config.setGroup("basic");
        List<VsConfig> configList = vsConfigMapper.selectVsConfigList(config);
        String appid = null;
        String sec_key = null;
        for (VsConfig v : configList) {
            if ("app_id".equals(v.getName())) {
                appid = v.getValue();
            }
            if ("sec_key".equals(v.getName())) {
                sec_key = v.getValue();
            }
        }
        TransApi api = new TransApi(appid,sec_key);
        TranslateUtil util = new TranslateUtil();
        String result = api.getTransResult(s, "auto", languageType);
        return util.conversion(result);
    }

    /**
     * 导出用户提现列表
     */
//    @ApiOperation("导出用户提现列表")
//    @Log(title = "用户提现", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, VsDramasUserWalletApply vsDramasUserWalletApply)
//    {
//        List<VsDramasUserWalletApply> list = vsDramasUserWalletApplyService.selectVsDramasUserWalletApplyList(vsDramasUserWalletApply);
//        ExcelUtil<VsDramasUserWalletApply> util = new ExcelUtil<VsDramasUserWalletApply>(VsDramasUserWalletApply.class);
//        util.exportExcel(response, list, "用户提现数据");
//    }

    /**
     * 获取用户提现详细信息
     */
    @ApiOperation("获取用户提现详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vsDramasUserWalletApplyService.selectVsDramasUserWalletApplyById(id));
    }

    /**
     * 新增用户提现
     */
    @ApiOperation("新增用户提现")
    @Log(title = "用户提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsDramasUserWalletApply vsDramasUserWalletApply)
    {
        return toAjax(vsDramasUserWalletApplyService.insertVsDramasUserWalletApply(vsDramasUserWalletApply));
    }

    /**
     * 修改用户提现
     */
    @ApiOperation("修改用户提现")
    @Log(title = "用户提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsDramasUserWalletApply vsDramasUserWalletApply)
    {
        return toAjax(vsDramasUserWalletApplyService.updateVsDramasUserWalletApply(vsDramasUserWalletApply));
    }

    /**
     * 删除用户提现
     */
    @ApiOperation("删除用户提现")
    @Log(title = "用户提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vsDramasUserWalletApplyService.deleteVsDramasUserWalletApplyByIds(ids));
    }

    /**
     * 计算用户提现额度
     */
    @PostMapping("/calculateConversionRatio")
    public AjaxResult calculateConversionRatio(@RequestBody VsDramasUserWalletApply vsDramasUserWalletApply) {
        if (getLoginUser().getVsUser() == null) {
            return error("暂未登录！");
        }
        vsDramasUserWalletApply.setUserId(getLoginUser().getVsUser().getId());
        return vsDramasUserWalletApplyService.calculateConversionRatio(vsDramasUserWalletApply);
    }

    @ApiOperation("用户提现")
    @PostMapping("/withdrawal")
    public AjaxResult withdrawal(@RequestBody VsDramasUserWalletApply vsDramasUserWalletApply) {
        if (getLoginUser().getVsUser() == null) {
            return error("暂未登录！");
        }
        vsDramasUserWalletApply.setUserId(getLoginUser().getVsUser().getId());
        return toAjax(vsDramasUserWalletApplyService.withdrawal(vsDramasUserWalletApply));
    }

}
